//
//  CalculateLoanChartsView.m
//  YiJuZhangShou
//
//  Created by URANUS on 16/3/18.
//  Copyright © 2016年 Uranus. All rights reserved.
//

#import "CalculateLoanChartsView.h"
#import "Masonry.h"
@import Charts;

@interface CalculateLoanChartsView()
@property (nonatomic, strong)LineChartView *chartView;
@end

@implementation CalculateLoanChartsView

#pragma mark Initialize
- (instancetype)init
{
    self = [super init];
    if (self) {
        [self loadCalculateLoanChartsView];
    }
    return self;
}

#pragma mark Private
- (void)loadCalculateLoanChartsView{
    [self.chartView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self).insets(UIEdgeInsetsMake(10, 10, 10, 10));
    }];
}

#pragma mark Public
- (void)setChartViewMoveToXPointAtIndex:(NSInteger)index{
    if (index >= 1) {
        [self.chartView moveViewToX:(index-1)];
    }
}

#pragma mark Getter
- (LineChartView *)chartView{
    if (_chartView == nil) {
        _chartView = [[LineChartView alloc] init];
        _chartView.descriptionText=@"";
        _chartView.noDataText=@"没有数据";
        [_chartView setScaleEnabled:YES];
        _chartView.dragEnabled = YES;
        _chartView.drawGridBackgroundEnabled = YES;
        
        ChartYAxis *leftAxis = _chartView.leftAxis;
        [leftAxis removeAllLimitLines];
        leftAxis.customAxisMax = 220.0;
        leftAxis.customAxisMin = -50.0;
        leftAxis.startAtZeroEnabled = NO;
        leftAxis.gridLineDashLengths = @[@5.f, @5.f];
        leftAxis.drawZeroLineEnabled = NO;
        leftAxis.drawLimitLinesBehindDataEnabled = YES;
        
        _chartView.rightAxis.enabled = NO;
        
        [_chartView.viewPortHandler setMaximumScaleY: 2.f];
        [_chartView.viewPortHandler setMaximumScaleX: 4.f];
        
        _chartView.legend.form = ChartLegendFormLine;
        
        [_chartView animateWithXAxisDuration:2.5 easingOption:ChartEasingOptionEaseInOutQuart];
        
        [self addSubview:_chartView];
    }
    return _chartView;
}

#pragma mark Setter
- (void)setLoanMoneyArray:(NSMutableArray *)loanMoneyArray{
    NSMutableArray *xVals = [[NSMutableArray alloc] init];
    
    for (int i = 0; i <= loanMoneyArray.count; i++)
    {
        [xVals addObject:[@(i) stringValue]];
    }
    
    NSMutableArray *yVals = [[NSMutableArray alloc] init];
    double maxVal = 0.0;
    double minVal = [loanMoneyArray[0] doubleValue];
    for (int i = 0; i < loanMoneyArray.count; i++)
    {
        double val = [loanMoneyArray[i] doubleValue];
        maxVal = MAX(maxVal, val);
        minVal = MIN(minVal, val);
        [yVals addObject:[[ChartDataEntry alloc] initWithValue:val xIndex:(i+1)]];
    }
    
    ChartYAxis *leftAxis = _chartView.leftAxis;
    [leftAxis removeAllLimitLines];
    leftAxis.customAxisMax = maxVal+100;
    leftAxis.customAxisMin = minVal-100;
    
    LineChartDataSet *set1 = [[LineChartDataSet alloc] initWithYVals:yVals label:@"月还款"];
    
    set1.lineDashLengths = @[@5.f, @2.5f];
    set1.highlightLineDashLengths = @[@5.f, @2.5f];
    [set1 setColor:UIColor.blackColor];
    [set1 setCircleColor:UIColor.blackColor];
    set1.lineWidth = 1.0;
    set1.circleRadius = 3.0;
    set1.drawCircleHoleEnabled = NO;
    set1.valueFont = [UIFont systemFontOfSize:9.f];
    
    NSArray *gradientColors = @[
                                (id)[ChartColorTemplates colorFromString:@"#00ff0000"].CGColor,
                                (id)[ChartColorTemplates colorFromString:@"#ffff0000"].CGColor
                                ];
    CGGradientRef gradient = CGGradientCreateWithColors(nil, (CFArrayRef)gradientColors, nil);
    
    set1.fillAlpha = 1.f;
    set1.fill = [ChartFill fillWithLinearGradient:gradient angle:90.f];
    set1.drawFilledEnabled = YES;
    
    NSMutableArray *dataSets = [[NSMutableArray alloc] init];
    [dataSets addObject:set1];
    
    LineChartData *data = [[LineChartData alloc] initWithXVals:xVals dataSets:dataSets];
    
    self.chartView.data = data;
    [self.chartView setVisibleXRangeMaximum:6];
}

@end
